package com.cloudspace.yunloan.pdf;

import android.app.Activity;
import android.graphics.pdf.PdfDocument;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;

import com.cloudspace.yunloan.R;
import com.cloudspace.yunloan.ext.aq.AQUtility;
import com.cloudspace.yunloan.utils.Utility;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class PdfSaveActivity extends Activity {
    private LinearLayout mRoot;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_pdf_save);
        mRoot = findViewById(R.id.root);
        AQUtility.postDelayed(new Runnable() {
            @Override
            public void run() {
                pdfSave(mRoot);
            }
        }, 1000);

    }

    private void pdfSave(View view){
        PdfDocument document = new PdfDocument();//1, 建立PdfDocument
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(
                view.getMeasuredWidth(), view.getMeasuredHeight(), 1).create();//2
        PdfDocument.Page page = document.startPage(pageInfo);
        view.draw(page.getCanvas());//3
        document.finishPage(page);

        File file = new File(Utility.getSDPath()+"/" + "PDFSample.pdf");
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            document.writeTo(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }



    }
}
